package lily.phs.urh;

import lily.phs.database.Constants;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity implements Constants {

	private EditText etName, etPassword;
	private CheckBox cbHoldPwd, cbAutoLogin;
	private Button btnLogin;
	private ProgressDialog mpDialog;
	private Resources res;
	private int serial=-1;
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(this)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setMessage("确定退出本系统吗？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			}
		})
		.create().show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		res = getResources();
		
		etName = (EditText)this.findViewById(R.id.user_name);
		etPassword = (EditText)this.findViewById(R.id.user_pwd);
		cbHoldPwd = (CheckBox)this.findViewById(R.id.hold_pwd);
		cbHoldPwd.setChecked(true);
		cbAutoLogin = (CheckBox)this.findViewById(R.id.auto_login);
		cbAutoLogin.setChecked(true);
		btnLogin = (Button)this.findViewById(R.id.login);
		
		
		final String[] login_ids = res.getStringArray(R.array.area_codes);
		//自动登录
		SharedPreferences settings = this.getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
		String idHold = settings.getString("area_code", "");
		Log.i("login", "name code in hold="+idHold);
		for(String id:login_ids){
			if(id.equals(idHold)){
				startActivity(new Intent(this,MainActivity.class));
				finish();
				break;
			}
		}
		
		btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String name_code = etName.getText().toString();
				if(name_code==null||name_code.length()==0){
					Toast.makeText(Login.this, "请输入用户名", Toast.LENGTH_LONG).show();
				}
				else{
					for(int i=0;i<login_ids.length;i++){
						if(login_ids[i].equals(name_code)){
							serial = i;
							break;
						}
					}
					if(serial==-1){
						Toast.makeText(Login.this, "用户名不存在", Toast.LENGTH_LONG).show();
					}
					else{
						Log.i("login", "serial="+serial);
						mpDialog = new ProgressDialog(Login.this);  
						mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置风格为圆形进度条  
						mpDialog.setMessage("正在登录...");  
						mpDialog.setIndeterminate(false);//设置进度条是否为不明确  
						mpDialog.setCancelable(true);//设置进度条是否可以按退回键取消  
						mpDialog.show();
						new LoginThread(name_code).start();
					}
				}
			}
		});
		
	}
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(msg.what==1){
				mpDialog.cancel();
				Intent intent = new Intent(Login.this, MainActivity.class);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
			}
		}
		
	};
	
	class LoginThread extends Thread{
		
		private String name_code;
		
		LoginThread(String name){
			this.name_code = name;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			final String[] ids = res.getStringArray(R.array.operator_ids);
			final String[] names = res.getStringArray(R.array.operaor_names);
			final String[] org_keys = res.getStringArray(R.array.org_keys);
			final String[] doctor_keys = res.getStringArray(R.array.doctor_keys);
			final String[] villages = res.getStringArray(R.array.def_villages);
			final String[] org_codes = res.getStringArray(R.array.org_codes);
			
			SharedPreferences uinfo = getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
			uinfo.edit()
			.putString("operator_name", names[serial])
			.putString("operator_id", ids[serial])
			.putString("org_key", org_keys[serial])
			.putString("doctor_key", doctor_keys[serial])
			.putString("def_village", villages[serial])
			.putString("org_code", org_codes[serial])
			.putString("area_code", name_code)
			.commit();
			Log.i("login", "operator_name="+names[serial]);
			Log.i("login", "operator_id="+ids[serial]);
			Log.i("login", "org_key="+org_keys[serial]);
			Log.i("login", "doctor_key="+doctor_keys[serial]);
			Log.i("login", "def_village="+villages[serial]);
			Log.i("login", "org_code="+org_codes[serial]);
			Log.i("login", "area_code="+name_code);
			
			Message message = handler.obtainMessage();
			message.what=1;
			handler.sendMessage(message);
		}
			
		}
	
}

